//
//  RankListViewController.m
//  edu
//
//  Created by baobaodp on 13-8-27.
//  Copyright (c) 2013年 baobaodp. All rights reserved.
//

#import "RankListViewController.h"
#import "RankListCell.h"

#import "ScoreListViewController.h"
#import "EduViewController.h"
#import "DBbase.h"
#import "Record.h"

@interface RankListViewController ()

@end

@implementation RankListViewController

@synthesize condition;
@synthesize rank_Div;

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Custom initialization
        self.isWithSearchBar = TRUE;
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
   
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark Table Data Source Methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 56;
}
- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    [self getRecords];
    return [self.datas count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"RankListCellIdentifier";
    
    RankListCell *cell = (RankListCell *)[tableView
                                    dequeueReusableCellWithIdentifier: cellIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"RankListCell"
                                                     owner:self options:nil];
        for (id oneObject in nib)
            if ([oneObject isKindOfClass:[RankListCell class]])
                cell = (RankListCell *)oneObject;
    }
	
    
    NSUInteger row = [indexPath row];
    [cell setData:[self.datas objectAtIndex:row] index:row];
    
    [self configureCell:cell indexPath:indexPath accessoryType:DISCLOSURE_INDICATOR];
    
    return cell;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // Navigation logic may go here. Create and push another view controller.
	
	ScoreListViewController *detailViewController = [[ScoreListViewController alloc] initWithNibName:@"ScoreListViewController" bundle:nil];
    detailViewController.isSelfInfo = false;
    detailViewController.condition = self.condition;
    
    
    NSUInteger row = [indexPath row];
    Record *data =[self.datas objectAtIndex:row];

    
    Record *student = [[Record alloc]initWithTableName:@"M_Student"];
    [student addKeyField:@"stu_Id" value:[data getFieldValueByName:@"stu_Id"]];
    [student loadFromDB];
    
    detailViewController.studentInfo = student;

    
	// Pass the selected object to the new view controller.
	
	if (tableView == self.searchDisplayController.searchResultsTableView){
        //		detailViewController.currentLineName = [filteredListContent objectAtIndex:indexPath.row];
        //		detailViewController.currentLineIndex = [[CDataContainer Instance].lineNameArray indexOfObject:[filteredListContent objectAtIndex:indexPath.row]];
	}
	else{
        //		detailViewController.currentLineName = [[CDataContainer Instance].lineNameArray objectAtIndex:indexPath.row];
        //		detailViewController.currentLineIndex = indexPath.row;
	}
    
	[self.navigationController pushViewController:detailViewController animated:YES];
    
    
}

-(void)getRecords
{
    
    NSString *ptn_Id = [self.condition getFieldValueByName:@"ptn_Id"];
    
    
    NSString *class_Id = [userEntry.userInfo getFieldValueByName:@"class_Id"];
    NSString *class_Ym = [userEntry.userInfo getFieldValueByName:@"class_Ym"];
    NSString *class_Div = [userEntry.userInfo getFieldValueByName:@"class_Div"];
    
    NSString *querySql = [NSString stringWithFormat:@"select T1.* ,T2.stu_Name ,T2.stu_Img from T_Rank T1 left join M_Student T2  on T1.stu_Id = T2.stu_Id  where T1.ptn_Id ='%@' and (T1.class_Id = '%@' ) and T1.rank_Type='2' and T1.rank_Div='1' order by t1.rank " ,ptn_Id,class_Id];
    
    if([self.rank_Div isEqualToString:@"2"]){
        querySql = [NSString stringWithFormat:@"select T1.* ,T2.stu_Name ,T2.stu_Img from T_Rank T1 left join M_Student T2  on T1.stu_Id = T2.stu_Id  where T1.ptn_Id ='%@' and ( ( T1.class_Ym = '%@' and T1.class_Div = '%@')) and T1.rank_Type='2' and T1.rank_Div='2' order by t1.rank " ,ptn_Id,class_Ym,class_Div];
    }

    const char *sql = [querySql UTF8String];
    
   self.datas = getDbRecords(sql,@"T_Rank",[NSArray arrayWithObjects:@"Rank_Id", nil]);
    
}

@end
